#include "Graphic.h"
Graphic::Graphic()
{
	sm_d3d=NULL;
	sm_device=NULL;
	sm_surface=NULL;
	sm_backbuffer=NULL;
}
bool Graphic::InitDevice(HWND p_handle)
{
	sm_d3d = Direct3DCreate9(D3D_SDK_VERSION);
	D3DPRESENT_PARAMETERS d3dpp; 

	ZeroMemory( &d3dpp, sizeof(d3dpp) );

	d3dpp.Windowed   = TRUE; // FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferHeight = SCREEN_HEIGHT;
	d3dpp.BackBufferWidth = SCREEN_WIDTH;

	sm_d3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		p_handle,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&sm_device);

	if (sm_device==NULL) 
	{
		return 0;
	}

	sm_device->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&sm_backbuffer);

	// Create a small off-screen surface (will fill it contain in GameRun)
	int result =
		sm_device->CreateOffscreenPlainSurface(
		100,					// width 				
		100,					// height
		D3DFMT_X8R8G8B8,		// format
		D3DPOOL_DEFAULT,		// where? (VRAM or RAM)
		&sm_surface,
		NULL);

	return true;
}
LPDIRECT3DDEVICE9 Graphic::GetDevice()
{
	return sm_device;
}
LPDIRECT3DSURFACE9 Graphic::GetSurface()
{
	return sm_surface;
}
LPDIRECT3DSURFACE9 Graphic::GetBackBuffer()
{
	return sm_backbuffer;
}
void Graphic::Release()
{
	if (sm_d3d!=NULL)
	{
		sm_d3d->Release();
		sm_d3d=NULL;
	}
	if (sm_device!=NULL)
	{
		sm_device->Release();
		sm_device=NULL;
	}
}